if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "slam"

end

if ( CLIENT ) then

	SWEP.DrawAmmo			= false
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 75
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= false
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Destruction Kit"
	SWEP.Slot = 1
	SWEP.Slotpos = 2
	SWEP.IconLetter = "E"

end

SWEP.Base = "tp_base"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.ViewModel = "models/weapons/v_eq_flashbang.mdl"
SWEP.WorldModel = "models/weapons/w_eq_flashbang.mdl"

SWEP.Click = Sound("buttons/lightswitch2.wav")
SWEP.Deny = Sound("HL1/fvox/buzz.wav")
SWEP.Hammer = GameSounds.WoodHammer
SWEP.Drill = GameSounds.Drill
SWEP.Drop = Sound("weapons/c4/c4_plant.wav")
SWEP.Rem = Sound("weapons/357/357_spin1.wav")

SWEP.Anim = ACT_VM_PULLBACK_LOW

SWEP.BModels = {"models/props_debris/wood_chunk03a.mdl","models/props_debris/wood_chunk03b.mdl"}
SWEP.DModel = "models/dav0r/tnt/tnt.mdl"
SWEP.RModel = "models/props_c17/tools_pliers01a.mdl"

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
	util.PrecacheModel(self.DModel)
	util.PrecacheModel(self.RModel)
end 

function SWEP:GetUsedAmmoType()
	return "none"
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	if CLIENT then
		self:ReleaseGhost()
	end
	return true
end  

function SWEP:Holster()
	self:ReleaseGhost()
	return true
end

function SWEP:ReleaseGhost()
	if ( self.GhostEntity ) then
		if (!self.GhostEntity:IsValid()) then self.GhostEntity = nil return end
		self.GhostEntity:Remove()
		self.GhostEntity = nil
	end
	if ( self.GhostOffset ) then
		for k,v in pairs( self.GhostOffset ) do
			self.GhostOffset[k] = nil
		end
	end
end

function SWEP:MakeGhost( model, pos, angle )

	self.GhostEntity = ents.Create( "prop_physics" )
	
	if (!self.GhostEntity:IsValid()) then
		self.GhostEntity = nil
		return
	end
	
	self.GhostEntity:SetModel( model )
	self.GhostEntity:SetPos( pos )
	self.GhostEntity:SetAngles( angle )
	self.GhostEntity:Spawn()
	
	self.GhostEntity:SetSolid( SOLID_VPHYSICS );
	self.GhostEntity:SetMoveType( MOVETYPE_NONE )
	self.GhostEntity:SetNotSolid( true );
	self.GhostEntity:SetRenderMode( RENDERMODE_TRANSALPHA )
	self.GhostEntity:SetColor( 255, 255, 255, 200 )
	
end

function SWEP:UpdateGhost()

	if (self.GhostEntity == nil) then return end
	if (!self.GhostEntity:IsValid()) then self.GhostEntity = nil return end
	
	local tr = utilx.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local ang = (trace.HitNormal * -1):Angle() + Angle(0,0,90)
	local pos = trace.HitPos + trace.HitNormal
	
	local trlength = self.Weapon:GetOwner():GetPos() - trace.HitPos
	trlength = trlength:Length() 
	
	if trlength < 110 then
		self.GhostEntity:SetColor( 50, 255, 50, 200 )
	else
		self.GhostEntity:SetColor( 255, 50, 50, 200 )
	end
	
	if trace.HitWorld then
		self.GhostEntity:SetModel(self.DModel)
		self.GhostEntity:SetPos( pos + trace.HitNormal * 3 )
		self.GhostEntity:SetAngles( ang )
	elseif table.HasValue(self.BModels,trace.Entity:GetModel()) then
		self.GhostEntity:SetModel(self.RModel)
		self.GhostEntity:SetPos(trace.HitPos + trace.HitNormal * 3)
		self.GhostEntity:SetAngles((trace.HitNormal * -1):Angle() + Angle(0,90,90))
	else
		self.GhostEntity:SetModel(self.DModel)
		self.GhostEntity:SetPos( pos + trace.HitNormal * 3 )
		self.GhostEntity:SetAngles( ang )
	end
	
end

function SWEP:SetPlacePosition(ent)

	local tr = utilx.GetPlayerTrace( self:GetOwner(), self:GetOwner():GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	
	local ang = (trace.HitNormal * -1):Angle() + Angle(0,0,90)
	ent:SetAngles( ang )
	
	local pos = trace.HitPos + trace.HitNormal * 3
	ent:SetPos( pos )
	//ent:SetPos(self.Owner:GetPos())
	
	local phys = ent:GetPhysicsObject()
	if phys:IsValid() then
		phys:EnableMotion( false )
	end

end

function SWEP:DrawHUD()
	
	if not self.GhostEntity then
		self:MakeGhost(self.DModel,self.Owner:GetPos() + Vector(0,0,100), Angle(0,0,0))
	else
		self:UpdateGhost()
	end

	local w,h = ScrW(),ScrH()
	
	surface.SetDrawColor(220, 180, 0, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
end

function SWEP:Think()
	if not self.BuildTime then return end
	if self.BuildTime < CurTime() then
		self:BarricadeTrace()
		self.BuildTime = nil
	end
end

function SWEP:CanPrimaryAttack()
	return true
end

function SWEP:BarricadeTrace()

	if CLIENT then return end

	local trace = util.GetPlayerTrace(self.Owner)
	local tr = util.TraceLine(trace)
	local trlength = self.Weapon:GetOwner():GetPos() - tr.HitPos
	trlength = trlength:Length() 
	   
	if not string.find(tr.Entity:GetClass(),"prop_phys") and trlength < 110 then
				
		if self.Owner.CurDyn then
			Inventory.Notice(self.Owner, "You already placed explosives", Inventory.Red)
			self.Owner:EmitSound(self.Deny)
		else
			self.Owner:TakeStamina(10)
			//self.Owner:AddAwardPoints(AWARD_GOOD_ENGINEER,1)
			self.Owner:EmitSound(self.Drop,100,math.random(90,110))
			Inventory.Notice(self.Owner, "Explosives planted", Inventory.Green)
			if not self.Owner.ExplosiveHint then
				self.Owner.ExplosiveHint = true
				Inventory.Notice(self.Owner, "To light the fuse, press E", Inventory.White)
			end
				
			local item = ents.Create("bomb_dynamite")
			item:SetModel(self.DModel)
			item:Spawn()
			item:SetPwner(self.Owner)
			self:SetPlacePosition(item)
			self.Owner.CurDyn = item
		end
		
	elseif table.HasValue(self.BModels,tr.Entity:GetModel()) and trlength < 110 then
	
		if tr.Entity.Builder == self.Owner then
			Inventory.PickupEffect(tr.Entity)
			Inventory.RemoveEnt(tr.Entity,true)
			local item = ents.Create("prop_physics")
			item:SetModel("models/props_debris/wood_chunk04a.mdl")
			item:Spawn()
			item:SetPos(tr.HitPos)
			self.Owner:EmitSound(self.Rem)
			Inventory.Notice(self.Owner, "Barricade removed", Inventory.Green)
		else
			Inventory.Notice(self.Owner, "That isn't your barricade", Inventory.Red)
			self.Owner:EmitSound(self.Deny)
		end
		
	else
		Inventory.Notice(self.Owner, "You aren't close enough", Inventory.Red)
		self.Owner:EmitSound(self.Deny)
	end
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
end

SWEP.PrimaryDelay = 1
function SWEP:CanPrimaryAttack()
	return CurTime() > self.PrimaryDelay
end

function SWEP:PrimaryAttack()
	if not self:CanPrimaryAttack() then return end
	self.PrimaryDelay = CurTime() + 1.5
	self.Weapon:SendWeaponAnim(self.Anim)
	self.BuildTime = CurTime() + .9
 end
 
function SWEP:SecondaryAttack()
	return
end

